package com.example.tuter.util.ssfwUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.GridLayout;
import android.widget.TextView;

import com.example.tuter.R;
import com.example.tuter.entity.CourseInformation;
import com.example.tuter.fragment.entity.ClassWeek;
import com.example.tuter.fragment.entity.CoursesKB;
import com.example.tuter.util.Constant;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Objects;

public class CourseUtil {


    //获取课程
    public static CoursesKB getCourse(String html) {
        if (html == null) return null;
        Document doc = Jsoup.parse(html);

        Element titleElement = doc.getElementsByTag("caption").get(0);
        CoursesKB coursesKB = new CoursesKB();
        coursesKB.title = titleElement.ownText();
        //检测是否返回课表信息
        if (coursesKB.title.indexOf(" 同学课表") == 0) return null;
        Elements trs = doc.getElementsByTag("tr");
        coursesKB.course = getCourses(trs);
        coursesKB.nonCourse = getNotCourse(trs);
        return coursesKB;
    }

    //获取非放置课程
    private static ArrayList<CourseInformation> getNotCourse(Elements trs) {
        ArrayList<CourseInformation> notCourse = new ArrayList<>();
        if (trs.size() > 12) {
            Element tr = trs.get(12);
            Elements sonTrs = tr.getElementsByTag("tr");
            for (int i = 2; i < sonTrs.size(); i++) {
                CourseInformation c = new CourseInformation();
                c.setCourseName(sonTrs.get(i).children().get(0).text().split("\\[")[0]);
                c.setWeeks(sonTrs.get(i).children().get(1).text());
                c.setTeacherName(sonTrs.get(i).children().get(2).text());
                c.setNote(sonTrs.get(i).children().get(3).text());
                notCourse.add(c);
            }
        }
        return notCourse;
    }

    //获取课程
    private static ArrayList<CourseInformation> getCourses(Elements trs) {

        if (trs.isEmpty()) return null;

        ArrayList<CourseInformation> courses = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            if (i % 2 == 0) continue;
            //第i/2节
            Elements tds = trs.get(i).children();
            for (int j = 2; j < tds.size(); j++) {
                String text = tds.get(j).text();
                if (!text.equals("")) {
                    String[] s = text.split(" ");
                    CourseInformation c = new CourseInformation();
                    c.setCourseName(s[0].split("\\[")[0]);
                    c.setWeeks(s[1]);
                    c.setTimes(s[2]);
                    c.setTeacherName(s[3]);
                    c.setLocation(s[4] + s[5]);
                    c.setDay(j - 1);
                    c.setTime((i + 1) / 2);
                    courses.add(c);
                    if (s.length > 6) {
                        //单双周的课
                        CourseInformation c2 = new CourseInformation();
                        c2.setCourseName(s[6].split("\\[")[0]);
                        c2.setWeeks(s[7]);
                        c2.setTimes(s[8]);
                        c2.setTeacherName(s[9]);
                        c2.setLocation(s[10] + s[11]);
                        c.setDay(j - 1);
                        c.setTime((i + 1) / 2);
                        courses.add(c2);
                    }
                }
            }
        }
        return courses;
    }

    /**
     * 填充课程表
     *
     * @param gridLayout
     */
    public static void setTable(GridLayout gridLayout, CoursesKB coursesKB, Context context) {
        if (coursesKB == null || coursesKB.course.isEmpty()) return;
        int now_week = Constant.CUR_WEEKS;//当前是当前学期的第几周

        for (CourseInformation course : coursesKB.course) {


            String coursename = course.getCourseName();    //课程名
            String courseweek = course.getWeeks();
            String courselocation = course.getLocation();

            ArrayList<ClassWeek> weeks = getWeeks(courseweek);
            for (ClassWeek classWeek : weeks) {
                //如果
                if (classWeek.state == -1 || now_week % 2 == classWeek.state) {
                    int row = course.getTime();   //第几节
                    int column = course.getDay(); //第几天
                    @SuppressLint("RestrictedApi") TextView textView = new TextView(context);
                    android.widget.GridLayout.LayoutParams params = new android.widget.GridLayout.LayoutParams();
                    params.width = 0;
                    params.height = 0;
                    params.rowSpec = android.widget.GridLayout.spec(row, 1, 1f);
                    params.columnSpec = android.widget.GridLayout.spec(column, 1, 1f);
                    textView.setGravity(Gravity.CENTER);
                    textView.setBackgroundColor(Color.BLUE);
                    textView.setTextSize(11);
                    textView.setText(coursename + courselocation);

                    //背景颜色处理，应该是同样的课是同一种颜色，这里先不做处理
                    if (row % 2 == 0 && column % 2 != 0) {
                        textView.setBackgroundResource(R.drawable.kcb_textview);
                    } else if (row % 2 == 0 && column % 2 == 0) {
                        textView.setBackgroundResource(R.drawable.kcb_textview1);
                    } else if (row % 2 != 0 && column % 2 == 0) {
                        textView.setBackgroundResource(R.drawable.kcb_textview2);
                    } else if (row % 2 != 0 && column % 2 != 0) {
                        textView.setBackgroundResource(R.drawable.kcb_textview4);
                    } else {
                        textView.setBackgroundResource(R.drawable.kcb_textview3);
                    }


                    params.setMargins(1, 1, 1, 1);
                    gridLayout.addView(textView, params);
                    if (now_week >= classWeek.startWeek && now_week <= classWeek.endWeek) {

                    }
                }
            }
        }
    }

    /**
     * 获取单个课程的内容
     *
     * @param courseWeek
     * @return
     */
    private static ArrayList<ClassWeek> getWeeks(String courseWeek) {
        ArrayList<ClassWeek> weeks = new ArrayList<>();
        //"3-6周(双),9-12周(双)"  1-13周
        String[] weekStringArray = courseWeek.split(",");
        for (String s : weekStringArray) {
            //3-6周(双)
            ClassWeek classWeek = new ClassWeek();
            String[] s2 = s.split("周");
            classWeek.startWeek = Integer.valueOf(s2[0].split("-")[0]);
            classWeek.endWeek = Integer.valueOf(s2[0].split("-")[1]);
            classWeek.state = -1;
            if (s2.length > 1) {
                //存在单双周情况
                classWeek.state = Objects.equals(s2[1], "(双)") ? 0 : 1;
            }
            weeks.add(classWeek);
        }

        return weeks;
    }
}
